package longrise.phone.com.bjjt_henan.bindinginformation;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.longrise.LEAP.Base.IO.JSONSerializer;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.ILSMsgListener;
import com.longrise.android.LFView;
import com.longrise.bjjt.util.PrefUtils;

import longrise.phone.com.bjjt_henan.Adapter.MyCarAdapter;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;

/**
 * 我的机动车界面
 */
public class MyCarView extends LFView implements View.OnClickListener, ILSMsgListener {
    private Context context = null;
    private View view = null;
    private ListView listView;
    private View addLayout;

    private EntityBean[] beans;
    private MyCarAdapter adapter;

    public MyCarView(Context c, EntityBean[] bean) {
        super(c);
        this.context = c;
        this.beans = bean;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
        setData();
    }

    private void initView() {
        view = View.inflate(context, R.layout.view_mycar, null);
        //标题栏
        TextView titleTV = (TextView) view.findViewById(R.id.tv_title);
        titleTV.setText("我的机动车");
        view.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnDestroy();
                closeForm();
            }
        });
        listView = (ListView) view.findViewById(R.id.mycar_lv);
        addLayout = LayoutInflater.from(context).inflate(R.layout.mycar_footview, null, false);
        listView.addFooterView(addLayout);
    }

    private void regEvent(boolean b) {
        if (null != addLayout) {
            addLayout.setOnClickListener(b ? this : null);
        }
    }

    public void setData() {
        this.addILSMsgListener(this);

        adapter = new MyCarAdapter(context, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EntityBean bean = (EntityBean) v.getTag();
                bindCar(bean);
            }
        });
        adapter.setData(beans);
        listView.setAdapter(adapter);
    }


    private void bindCar(EntityBean bean) {
        BindCarView view = new BindCarView(context, bean);
        FrameworkManager.getInstance().showNewForm(context, view);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mycar_layout_add://添加机动车
                bindCar(null);
                break;
            default:
                break;
        }
    }

    @Override
    public Object onLSMsg(int i, Object... objects) {
        if (i == 101 || i == 102) {
            EntityBean resultBean = (EntityBean) JSONSerializer.getInstance().DeSerialize(PrefUtils.
                    getString(context, "carinfo", null), EntityBean.class);
            EntityBean dataBean = resultBean.getBean("data");
            if (null != dataBean) {
                beans = dataBean.getBeans("car");
                adapter.setData(beans);
                adapter.notifyDataSetChanged();
            }
        }
        return null;
    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        view = null;
        regEvent(false);
        removeILSMsgListener(this);
        DialogUtils.destroyDialog();
    }
}